--#include "data\config\activityconfig\BackStageActivity\TimeLimitShoppingConfig.lua" once
--#include "data\functions\Common\RankMgr.lua" once 		--排行榜管理
--#include "data\functions\Activity\BackStageActivity\BackStageActivity.lua"   once         -- 后台活动开启关闭管理

--限时商品开始
function StartTimeLimitShopping()
	local ssVar = System.getStaticVar()
	ssVar.timeLimitShopping = 1
	gRankMgr_Clear(RANK_DEFINE_TIME_LIMIT_SHOP)
	OpenBackStageActivity(CommonActivity_TimeLimitShopping)
end

--限时商品结束
function EndTimeLimitShopping()
	local ssVar = System.getStaticVar()
	ssVar.timeLimitShopping = nil
	CloseBackStageActivity(CommonActivity_TimeLimitShopping)
end



--限时商品元宝
function TimeLimitShoppingWithDrawYuanBao(sysarg, Gold)
	if not System.IsCommonActivityOpen(CommonActivity_TimeLimitShopping) then
		return
	end
	local ssVar = System.getStaticVar()
	if not ssVar.timeLimitShopping then
		return
	end
	local nPlanId = System.GetCommonActivityParam(CommonActivity_TimeLimitShopping,0,0,0,0)
	local cfg = TimeLimitShoppingCfg[nPlanId] 
	if not cfg then
		cfg = TimeLimitShoppingCfg[#TimeLimitShoppingCfg]
	end

	local itemIndex = ssVar.timeLimitShopping
	local actorid = Actor.getActorId(sysarg)
	local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_TIME_LIMIT_SHOP, itemIndex)

	local myData
	if data == "" or data == nil or data == "0" then
		myData = cfg.LimitCount
	else
		myData = tonumber(data)
	end

	if myData <= 0 then
		return
	end

	for idx = 1, #cfg.Gold do
		if cfg.Gold[idx] == Gold then
			myData = myData - 1
			if myData == 0 then
				myData = -1
			end
			gRankMgr_SetRankEx(actorid, RANK_DEFINE_TIME_LIMIT_SHOP, itemIndex, myData)

			TimeLimitShoppingSendMail(sysarg, idx)
			
			if myData == -1 then
				CloseBackStageActivity(CommonActivity_TimeLimitShopping, sysarg)
			end
			break
		end
	end
end

--发送限时商品数据
function SendTimeLimitShoppingData(sysarg, packet, myData)
	if not System.IsCommonActivityOpen(CommonActivity_TimeLimitShopping) then
		return
	end
	local ssVar = System.getStaticVar()
	if not ssVar.timeLimitShopping then
		return
	end
	local nPlanId = System.GetCommonActivityParam(CommonActivity_TimeLimitShopping,0,0,0,0)
	if nPlanId == 0 or nPlanId > #TimeLimitShoppingCfg then
		nPlanId = #TimeLimitShoppingCfg
	end
	local cfg = TimeLimitShoppingCfg[nPlanId] 

	if not myData then
		local itemIndex = ssVar.timeLimitShopping
		local actorid = Actor.getActorId(sysarg)
		local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_TIME_LIMIT_SHOP, itemIndex)

		if data == "" or data == nil or data == "0" then
			myData = cfg.LimitCount
		else
			myData = tonumber(data)
		end
	end


	local pack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sGetTimeLimitShoppingInfo)
	if (pack == nil) then
		return
	end
	DataPack.writeByte(pack, nPlanId)
	local LeftTime = System.GetCommonActivityLeftTime(CommonActivity_TimeLimitShopping)
	DataPack.writeUInt(pack, LeftTime or 0)
	if myData == -1 then
		myData = 0 
	end
	DataPack.writeByte(pack, myData or 0)
	DataPack.flush(pack)
end

--领取奖励
function TimeLimitShoppingSendMail(sysarg, Index)
	if not System.IsCommonActivityOpen(CommonActivity_TimeLimitShopping) then
		return
	end
	local ssVar = System.getStaticVar()
	if not ssVar.timeLimitShopping then
		return
	end
	local nPlanId = System.GetCommonActivityParam(CommonActivity_TimeLimitShopping,0,0,0,0)
	local cfg = TimeLimitShoppingCfg[nPlanId] 
	if not cfg then
		cfg = TimeLimitShoppingCfg[#TimeLimitShoppingCfg]
	end

	local rewardCfg = cfg.Rewards

	local rewards = rewardCfg[Index]

	
	local actorId = Actor.getActorId(sysarg)
	local title = Lang.ScriptTips.TimeLimitShopping001
	local content = Lang.ScriptTips.TimeLimitShopping002
	Awards.GiveToMailById(actorId, rewards, title, content, LogIds.TimeLimitShoppingLogId, Lang.LogTips.TimeLimitShopping)

	SendTimeLimitShoppingData(sysarg)
end

--判断每日商品是否全部领取
function GetTimeLimitShoppingState(sysarg)
	if not System.IsCommonActivityOpen(CommonActivity_TimeLimitShopping) then
		return true
	end
	local ssVar = System.getStaticVar()
	if not ssVar.timeLimitShopping then
		return true
	end
	local nPlanId = System.GetCommonActivityParam(CommonActivity_TimeLimitShopping,0,0,0,0)
	if nPlanId == 0 or nPlanId > #TimeLimitShoppingCfg then
		nPlanId = #TimeLimitShoppingCfg
	end
	local cfg = TimeLimitShoppingCfg[nPlanId] 

	local itemIndex = ssVar.timeLimitShopping
	local actorid = Actor.getActorId(sysarg)
	local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_TIME_LIMIT_SHOP, itemIndex)

	local myData
	if data == "" or data == nil or data == "0" then
		myData = cfg.LimitCount
	else
		myData = tonumber(data)
	end

	if myData == -1 then
		return true
	end
	return false
end